<?php

/*
Copyright 2007 Benjamin V King

BH Widgets is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

BH Widgets is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with BH Widgets; if not, write to the Free Software Foundation,
Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

require_once('BH_Widget_Base.php');


class BH_Links_Widget extends BH_Controllable_Widget_Base {
	
	static function get_categories($where ='') {
		global $wpdb;
		$sql = "select * from ". $wpdb->terms ." where term_id in (select term_id from ".$wpdb->term_taxonomy ." where taxonomy='link_category' and count > 0)";

		if ($where)
			$sql .= " and $where";

		$sql .=" order by name";
		return $wpdb->get_results($sql, OBJECT);
	}
	
	static function count_visible_links($termid) {
		global $wpdb;
		
		$sql  ="select count(*) from ". $wpdb->links;
		$sql .=" where link_id in";
		$sql .=" (select object_id from ". $wpdb->term_relationships ." where term_taxonomy_id in";
		$sql .=" (select distinct r.term_taxonomy_id from ". $wpdb->term_taxonomy ." t, ". $wpdb->term_relationships ." r";
		$sql .=" where t.term_id='".$termid."' and t.term_taxonomy_id=r.term_taxonomy_id)) and link_visible='Y'";
		
		return $wpdb->get_var($sql);
	}


	function widget($args) {
		global $userdata;
		extract($args);
		
		$all = $this->get_attribute('all');
		$selected_categories = $this->get_attribute('selected-categories');
		
		if (!$selected_categories && !$all) return;
		
		get_currentuserinfo();
		
		$where = $all ? '' : 'term_id in (\''. join("','", $selected_categories) .'\')';
		
		$save_name = $this->get_name();

		foreach(self::get_categories($where) as $category)  {
		
			if (!self::count_visible_links($category->term_id) )
				continue;
				
			$this->set_name($save_name . '-' . $category->slug);
			
			//add edit link if user is logged in
			$edit_link = $userdata->user_level ?
				' (<a href="/wp-admin/link-category.php?cat_ID='. $category->term_id .'&action=edit">edit</a>)' :
				'';

			echo $before_widget ."\n";
			echo $this->get_widget_start_tag($args);
			
			echo $before_title . $category->name . $edit_link . $after_title . "\n";
			echo $before_list ."\n";

			get_links($category->term_id,$before_item,$after_item,'<br/>',false, 'name',false);

			echo $after_list ."\n";
			echo $this->get_widget_end_tag($args);
			echo $after_widget . "\n";
		}
		
		$this->set_name($save_name);
	}

	function control() {
		if ( $this->get_request_param_value('submit') ) {
			$this->update_attribute_from_param('selected-categories');
			$this->update_attribute_from_param('all');
		}
		
		$selected_categories = $this->get_attribute('selected-categories');
		$all = $this->get_attribute('all');
		
		echo "<label>All Categories:</label><input type=\"checkbox\" name=\"" . $this->get_request_param_name('all') . "\" ". ($all=='1' ? 'checked' : '') ." value=\"1\"/><br/>\n";
		echo "<select name=\"" . $this->get_request_param_name('selected-categories') . "[]\" size=\"5\" multiple >\n";
	
		foreach(self::get_categories() as $category) {
			$selected='';
			if (in_array($category->term_id, $selected_categories)) {
				$selected='selected="true"';
			}
			
			echo "<option value=\"". $category->term_id ."\" ". $selected .">". $category->name ."</option><br/>\n";
		}
	
		echo "</select>\n";
		echo "<input type=\"hidden\" id=\"" . $this->get_request_param_name('submit') . "\" name=\"" . $this->get_request_param_name('submit') . "\" value=\"1\" />\n";
	}
}

bh_widgets_register_class('BH_Links_Widget');
?>
